﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Bussiness;

public partial class TemplateManage : System.Web.UI.Page
{
    TemplateFile tf = new TemplateFile();
    protected void Page_Load(object sender, EventArgs e)
    {
        setContent();
    }


    private void setContent()
    {
        Store1.DataSource = tf.GetList("").Tables[0].DefaultView;
        Store1.DataBind();

    }
    protected void UploadClick(object sender, DirectEventArgs e)
    {
        string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";

        if (this.FileUploadField1.HasFile)
        {
            this.FileUploadField1.PostedFile.SaveAs(Server.MapPath("Template//" + this.FileUploadField1.PostedFile.FileName));
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.INFO,
                Title = "Success",
                Message = string.Format(tpl, this.FileUploadField1.PostedFile.FileName, this.FileUploadField1.PostedFile.ContentLength)
            });
            TextField2.Text = this.FileUploadField1.PostedFile.FileName;
        }
        else
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.ERROR,
                Title = "Fail",
                Message = "No file uploaded"
            });
        }
    }
    protected void HandleChanges(object sender, BeforeStoreChangedEventArgs e)
    {
        ChangeRecords<TemplateFile> persons = e.DataHandler.ObjectData<TemplateFile>();

        foreach (TemplateFile created in persons.Created)
        {
            int tempId = created.id;
            tf.tempName = created.tempName;
            tf.fileName = created.fileName;
            tf.Type =  created.Type ;
            int newId = tf.Add();

            if (Store1.UseIdConfirmation)
            {
                e.ConfirmationList.ConfirmRecord(tempId.ToString(), newId.ToString());
            }
            else
            {
                Store1.UpdateRecordId(tempId, newId);
            }

        }

        foreach (TemplateFile deleted in persons.Deleted)
        {
            // this.DeletePerson(deleted.id);
            tf.Delete(deleted.id);
            if (Store1.UseIdConfirmation)
            {
                e.ConfirmationList.ConfirmRecord(deleted.id.ToString());
            }
        }

        foreach (TemplateFile updated in persons.Updated)
        {
            //this.UpdatePerson(updated);
            tf.id = updated.id;
            tf.tempName = updated.tempName;
            tf.fileName = updated.fileName;
            tf.Type = updated.Type;
            tf.Update();
            if (Store1.UseIdConfirmation)
            {
                e.ConfirmationList.ConfirmRecord(updated.id.ToString());
            }
        }
        e.Cancel = true;
    }
}